﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;

public partial class SuperManage_SectionInfo : System.Web.UI.Page
{

    private static SectionServices sectionServices = new SectionServices();
    private static SectionDAO sectionDAO = new SectionDAO();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dataBind();
            delSectionLib.Visible = false;
            addSectionLib.Visible = false;
            txtBaseSectionId.Value = "0";
            lblCurOperation.Text = "【部门添加】";
            submitBtn.Text = "添加";
            txtSectionName.Focus();
            sectionInfoTree.HoverNodeStyle.BackColor = System.Drawing.Color.LightSkyBlue;
            delSectionLib.Attributes.Add("onClick", "javascript:return confirm('您确定要删除此部门信息吗？')");
            ddlBaseSection.Attributes.Add("onChange", "javascript:document.getElementById('txtBaseSectionId').value = document.getElementById('ddlBaseSection').value");
        }
        if(null != Request["action"] && "show".Equals(Request["action"]))
        {
            showSectionInfo();
        }
    }

    //控件绑定
    private void dataBind()
    {
        intoDdlBaseSection();
        treeDataBind();
    }

    //将部门信息绑定到树控件
    public void treeDataBind()
    {
        sectionInfoTree.Nodes.Clear();
        TreeNode bootNode = new TreeNode("部门信息");
        bootNode.ImageUrl = "../images/Section.png";
        bootNode.SelectAction = TreeNodeSelectAction.Expand;
        sectionInfoTree.Nodes.Add(bootNode);
        foreach(Section section in sectionDAO.findByBaseSection(0))
        {
            TreeNode treeNode = new TreeNode(section.SectionName, section.SectionId.ToString());
            treeNode.NavigateUrl = "SectionInfo.aspx?sid=" + section.SectionId + "&action=show";
            treeNode.ImageUrl = "../images/Sections.png";
            treeNode.SelectAction = TreeNodeSelectAction.Expand;
            bootNode.ChildNodes.Add(treeNode);
            sectionTreePerform(section.SectionId, treeNode);
        }
    }

    //部门信息递归查询
    public void sectionTreePerform(int baseSectionId, TreeNode node)
    {
        List<Section> list = sectionDAO.findByBaseSection(baseSectionId);
        foreach (Section section in list)
        {
            TreeNode treeNode = new TreeNode(section.SectionName, section.SectionId.ToString());
            treeNode.NavigateUrl = "SectionInfo.aspx?sid=" + section.SectionId + "&action=show";
            treeNode.ImageUrl = "../images/Sections.png";
            treeNode.SelectAction = TreeNodeSelectAction.Expand;
            node.ChildNodes.Add(treeNode);
            if (list.Count == 0)
            {
                break;
            }
            sectionTreePerform(section.SectionId, treeNode);
        }
    }

    //删除部门信息方法
    public Boolean delSection(string sectionId)
    {
        int sid = int.Parse(sectionId);
        int bsid = int.Parse(txtBaseSectionId.Value);
        int curPlace = int.Parse(txtCurPlace.Value);
        return sectionServices.delSection(sid, bsid, curPlace);
    }

    //上级部门选择框填充
    public void intoDdlBaseSection()
    {
        ddlBaseSection.Items.Clear();
        ListItem item = new ListItem("一级部门", "0");
        ddlBaseSection.Items.Add(item);
        foreach (Section section in sectionServices.findParentSectionInfo())
        {
            ListItem listItem = new ListItem(section.SectionName, section.SectionId.ToString());
            ddlBaseSection.Items.Add(listItem);
        }
    }

    //将部门信息显示在文本框中
    public void showSectionInfo()
    {
        dataBind();
        int sid = int.Parse(Request["sid"]);
        Section section = sectionDAO.findBySectionId(sid);
        txtSectionId.Value = section.SectionId.ToString();
        txtCurPlace.Value = section.CurrentPlace.ToString();
        txtBaseSectionId.Value = section.BaseSection.SectionId.ToString();
        txtSectionName.Text = section.SectionName;
        txtSectionAddress.Text = section.Address;
        txtSectionFacsimile.Text = section.Facsimile;
        txtSectionRemark.Text = section.Remark;
        txtSectionTelephone.Text = section.Telephone;
        ddlBaseSection.Items.FindByValue(section.BaseSection.SectionId.ToString()).Selected = true;
        foreach(int ssid in sectionServices.findAllChildSectionByBaseSectionId(sid))
        {
            ddlBaseSection.Items.Remove(ddlBaseSection.Items.FindByValue(ssid.ToString()));
        }
        
        lblCurOperation.Text = "【部门更改】";
        submitBtn.Text = "更改";
        delSectionLib.Visible = true;
        addSectionLib.Visible = true;
    }

    protected void addSectionLib_Click(object sender, EventArgs e)
    {
        submitBtn.Text = "添加";
        lblCurOperation.Text = "【部门添加】";
        delSectionLib.Visible = false;
        addSectionLib.Visible = false;
        Response.Redirect("SectionInfo.aspx");
    }

    protected void submitBtn_Click(object sender, EventArgs e)
    {
        if("更改".Equals(submitBtn.Text))
        {
            //string sectionId = txtSectionId.Value;
            string sectionId = Request.Form.Get(txtSectionId.ClientID);
            //string sectionName = txtSectionName.Text;
            string sectionName = Request.Form.Get(txtSectionName.ClientID);
            //string telephone = txtSectionTelephone.Text;
            string telephone = Request.Form.Get(txtSectionTelephone.ClientID);
            //string facsimile = txtSectionFacsimile.Text;
            string facsimile = Request.Form.Get(txtSectionFacsimile.ClientID);
            string baseSectionId = Request["txtBaseSectionId"];
           // string remark = txtSectionRemark.Text;
            string remark = Request.Form.Get(txtSectionRemark.ClientID);
            //string address = txtSectionAddress.Text;
            string address = Request.Form.Get(txtSectionAddress.ClientID);
            int state = sectionServices.updateSectionInfo(sectionId, sectionName, telephone, baseSectionId, facsimile, remark, address);
            if(state == 1)
            {
                Response.Write("<script>alert('部门信息更新成功！');location.href='SectionInfo.aspx';</script>");
            }
            else if(state == 2)
            {
                Response.Write("<script>alert('不允许将自己添加到自己子部门下！');location.href='SectionInfo.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('部门信息更新失败，请检查网络！');location.href='SectionInfo.aspx';</script>");
            }
        }
        else if("添加".Equals(submitBtn.Text))
        {
            string sectionName = txtSectionName.Text;
            string telephone = txtSectionTelephone.Text;
            int baseSection = int.Parse(txtBaseSectionId.Value);
            string facsimile = txtSectionFacsimile.Text;
            string remark = txtSectionRemark.Text;
            string address = txtSectionAddress.Text;
            if(sectionServices.createSection(sectionName, telephone, baseSection, facsimile, remark, address))
            {
                Response.Write("<script>alert('部门信息添加成功！')</script>");
            }
            else
            {
                Response.Write("<script>alert('部门信息添加失败，请检查网络！')</script>");
            }
            Response.Redirect("SectionInfo.aspx");
        }
    }

    protected void delSectionLib_Click(object sender, EventArgs e)
    {
        if (null == txtSectionId.Value || "".Equals(txtSectionId.Value))
        {
            Response.Write("<script>alert('请选择部门信息！')</script>");
        }
        else
        {
            delSection(txtSectionId.Value);
        }
        dataBind();
        Response.Redirect("SectionInfo.aspx");
    }
   
}
